<template>
    <el-dialog @close="cancleEditDict" v-model="dialogVisible" title="编辑字典信息" destroy-on-close center>
        <template #default>
            <el-form :model="dict" label-width="100px" label-position="left">
                <el-form-item label="字典名称">
                    <el-input v-model="dict.dictName" aria-placeholder="字典名称" />
                </el-form-item>
                <el-form-item label="字典类型">
                    <el-input v-model="dict.dictType" aria-placeholder="字典类型" />
                </el-form-item>
                <el-form-item label="字典备注">
                    <el-input type="textarea" v-model="dict.dictNotes" aria-placeholder="字典备注" />
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="dict.dictStatus" class="ml-4">
                        <el-radio :value="1" >正常</el-radio>
                        <el-radio :value="2" >废弃</el-radio>
                        <el-radio :value="99" >删除</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </template>
        <!-- footer插槽中放两个按钮 -->
        <template #footer>
            <el-row>
                <el-col :span="12">
                    <el-button type="primary" @click="editDict">确定</el-button>
                </el-col>
                <el-col :span="12">
                    <el-button type="primary" @click="cancleEditDict">取消</el-button>
                </el-col>
            </el-row>
        </template>
    </el-dialog>
</template>
<script>
import { watch } from "vue";
import DictRequest from '../../../api/dict.js';
export default {
    props: ["visible","dictId"],
    emits: ["optionCompleted"], // 对外暴露一个事件，用来传递添加学生的操作结果
    data() {
        return {
            dict: {},// 绑定要编辑的学生信息
            dialogVisible: false,// 编辑学生窗口的显示与隐藏
        }
    },
     methods: {
        /**
         * 取消添加学生的事件
         */
        cancleEditDict(){
            // 通知父组件  操作完成，让父组件关闭窗体
            this.$emit("optionCompleted");
        },
        /**
         * 添加学生信息
         */
        editDict(){
            DictRequest.updateDict(this.dict,res=>{
                if (res.code == '200'){
                    this.$message({
                        type: 'success',
                        message: '编辑字典成功!'
                    });
                    // 通知父组件  操作完成，让父组件关闭窗体
                    this.$emit("optionCompleted");
                }else if(res.code == '300'){
                    this.$message({
                        type: 'warning',
                        message: '字典' + (res.msg.indexOf("dictName") > 0 ?"名称":"类型") + "重复"
                    });
                }else{
                    this.$message({
                        type: 'error',
                        message: '编辑字典失败!'
                    });
                }
            });
        },

        /**
         * 加载学生信息
         */
        loadDict(){
            DictRequest.getById(this.dictId,res=>{
                console.log(res)
                if(res.data != null)
                    this.dict = res.data;
            });
        }
    }, mounted(props) {
        this.loadDict()
        // 侦听props   一旦发生变化，就会出发下面的函数
        watch(() => this.visible, (newVal, oldVal) => {
            // 将最新的值设置给弹窗的dialogVisible属性
            this.dialogVisible = newVal;
        });

        watch(() => this.dictId, (newVal, oldVal) => {
            // 如果dictId 发生变化，重新加载字典信息
            this.loadDict();
        });
    }
}
</script>